package com.example.demo.dynamicproxy.client;

import com.example.demo.dynamicproxy.v2.DemoClassLoader;
import com.example.demo.dynamicproxy.v2.DemoInvocationHandler;
import com.example.demo.dynamicproxy.v2.DemoProxy;

import java.lang.reflect.Method;

/**
 * @author LuoZhenZhong
 * @date 2020/3/4
 */
public class PersonProxyV2 implements DemoInvocationHandler {

    private IPerson target;

    public Object createObject(IPerson target) {
        this.target = target;
        try {
            return DemoProxy.newProxyInstance(new DemoClassLoader(), target.getClass().getInterfaces(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(target, args);
        after();
        return invoke;
    }

    private void after() {
        System.out.println("代理： 发言之后");
    }

    private void before() {
        System.out.println("代理： 发言之前");
    }
}
